package com.lzx.iteam;

import android.app.AlertDialog;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.MotionEvent;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.*;
import com.lzx.iteam.base.FindViewById;
import com.lzx.iteam.base.MyBaseActivity;
import com.lzx.iteam.bean.PayToolsData;
import com.lzx.iteam.bean.PayToolsGroupData;
import com.lzx.iteam.net.*;
import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;

import java.util.ArrayList;
import java.util.List;
import java.util.regex.Pattern;

import static android.R.attr.id;

/**
 * Created by Megan on 16/9/18.
 */
public class TeamPermissionActivity extends MyBaseActivity implements View.OnClickListener {
    @FindViewById(id = R.id.title_tv_right)
    private TextView mTvRight;
    @FindViewById(id = R.id.title_tv_center)
    private TextView mTvTitle;
    @FindViewById(id = R.id.title_ll_back)
    private LinearLayout mLayoutBack;
    @FindViewById(id = R.id.tv_team_contact_count)
    private TextView mTvTeamContactCount;
    @FindViewById(id = R.id.tv_attendance_contact_count)
    private TextView mTvAttendanceContactCount;
    @FindViewById(id = R.id.tv_attendance_count)
    private TextView mTvAttendanceCount;
    @FindViewById(id = R.id.tv_legwork_state)
    private TextView mTvLegworkState;
    @FindViewById(id = R.id.tv_weekly_count)
    private TextView mTvWeeklyCount;
    @FindViewById(id = R.id.tv_weekly_template_count)
    private TextView mTvWeeklyTemplateCount;
    @FindViewById(id = R.id.tv_approval_contact_count)
    private TextView mTvApprovalContactCount;
    @FindViewById(id = R.id.tv_team_permission_pay)
    private TextView mTvPayUpgrade;
    private String mGroupId;
    private final int MSG_UPGRADE_ACCOUNT = 1001;
    private final int MSG_TEAM_PERMISSION = 1002;

    private Handler mHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case MSG_UPGRADE_ACCOUNT:
                    if (msg.arg1 == 0) {
                        String status=(String)msg.obj;
                        if("1".equals(status)){
                            Toast.makeText(mActivity, "升级成功",
                                    Toast.LENGTH_LONG).show();
                        }else{
                            Toast.makeText(mActivity, "升级失败",
                                    Toast.LENGTH_LONG).show();
                        }

                    } else if (msg.arg1 == AsynHttpClient.ERROR_LOCAL) {
                        Toast.makeText(mActivity, R.string.active_failure_no_network,
                                Toast.LENGTH_LONG).show();
                    } else {
                        Toast.makeText(mActivity, (String) msg.obj,
                                Toast.LENGTH_LONG).show();
                    }
                    break;
                case MSG_TEAM_PERMISSION:
                    if (msg.arg1 == 0) {
                        PayToolsGroupData data = (PayToolsGroupData) msg.obj;
                        mTvTeamContactCount.setText(data.getMember_limit() + "人");
                        mTvAttendanceContactCount.setText(data.getAttendance_member_limit() + "人");
                        mTvAttendanceCount.setText(data.getAttendance_count_limit() + "处");
                        if (data.getLegwork_status() == 0) {
                            mTvLegworkState.setText("关闭");
                        } else {
                            mTvLegworkState.setText("开启");
                        }
                        mTvWeeklyCount.setText(data.getWeekly_member_limit() + "条");
                        mTvWeeklyTemplateCount.setText(data.getWeekly_templ_limit() + "个");
                        mTvApprovalContactCount.setText(data.getApproval_admin_limit() + "人");

                    } else if (msg.arg1 == AsynHttpClient.ERROR_LOCAL) {
                        Toast.makeText(mActivity, R.string.active_failure_no_network,
                                Toast.LENGTH_LONG).show();
                    } else {
                        Toast.makeText(mActivity, (String) msg.obj,
                                Toast.LENGTH_LONG).show();
                    }
                    break;

            }
        }
    };


    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState, R.layout.team_permission_layout);


    }

    @Override
    public void initListener() {
        mLayoutBack.setOnClickListener(this);
        mTvRight.setOnClickListener(this);
        mTvPayUpgrade.setOnClickListener(this);
    }

    @Override
    public void initData() {
        mTvTitle.setText("团队权限详情");
        Intent intent = getIntent();
        mGroupId = intent.getStringExtra("group_id");
        mTvRight.setVisibility(View.VISIBLE);
        mTvRight.setText("升级码");
        getTeamPermissionMsg();
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.title_ll_back:
                finish();
                break;
            case R.id.title_tv_right:
                showUpgradeWindow();
                break;
            case R.id.tv_team_permission_pay:
                Toast.makeText(TeamPermissionActivity.this,"暂未开通,请耐心等待",Toast.LENGTH_LONG).show();
                /*Intent intent=new Intent(TeamPermissionActivity.this,PayToolsActivity.class);
                startActivity(intent);*/
                break;
        }
    }

    private void showUpgradeWindow() {

        final AlertDialog dialog = new AlertDialog.Builder(this).create();
        dialog.setView(new EditText(this));
        dialog.show();
        Window window = dialog.getWindow();
        WindowManager.LayoutParams lp = dialog.getWindow().getAttributes();
        lp.dimAmount = 0.3f; // TODO 设置黑暗度  0.0f完全不暗，1.0f全暗
//        lp.alpha = 0.5f; // TODO 设置透明度    0.0f完全透明，1.0完全不透明
        window.setAttributes(lp);
        // 设置布局
        window.setContentView(R.layout.popwindow_accound_upgrade);
        // 设置宽高
        window.setLayout(LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.MATCH_PARENT);
        // 设置弹出的动画效果
        window.setWindowAnimations(R.style.bottomStyle);
        final EditText edit = (EditText) window.findViewById(R.id.et_accound_upgrade_edit);
        ImageView upgrade = (ImageView) window.findViewById(R.id.iv_accound_upgrade_btn);
        TextView close = (TextView) window.findViewById(R.id.tv_accound_upgrade_close);

        close.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                dialog.cancel();
            }
        });
        upgrade.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String code = edit.getText().toString().trim();
                if (Pattern.matches("[a-z]{3}-[a-z0-9]{8}\\d{4}", code)) {
                    upgradeAccount(code);
                    dialog.cancel();
                } else {
                    Toast.makeText(TeamPermissionActivity.this, "升级码格式错误", Toast.LENGTH_LONG).show();
                }
            }
        });

        LinearLayout cancelLayout = (LinearLayout) window.findViewById(R.id.view_cancel);
        cancelLayout.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View view, MotionEvent motionEvent) {
                dialog.cancel();
                return false;
            }
        });
    }

    private void upgradeAccount(String code) {
        List<NameValuePair> mList = new ArrayList<NameValuePair>();
        mList.add(new BasicNameValuePair(AsynHttpClient.KEY_SID, LocalLogin.getInstance().mSid));
        mList.add(new BasicNameValuePair("code", code));
        mList.add(new BasicNameValuePair("group_id", mGroupId));
        GetStatusMsg message = new GetStatusMsg(mHandler.obtainMessage(MSG_UPGRADE_ACCOUNT));
        message.mApi = AsynHttpClient.API_PERMISSION_UPGRADE_CODE;
        message.mParams = mList;
        AsynHttpClient.getInstance(this).execUserHttp(message);

    }

    private void getTeamPermissionMsg() {
        List<NameValuePair> mList = new ArrayList<NameValuePair>();
        mList.add(new BasicNameValuePair(AsynHttpClient.KEY_SID, LocalLogin.getInstance().mSid));
        mList.add(new BasicNameValuePair("group_id", mGroupId));
        GetTeamPermissionMsg message = new GetTeamPermissionMsg(mHandler.obtainMessage(MSG_TEAM_PERMISSION));
        message.mApi = AsynHttpClient.API_PERMISSION_GROUP_INFO;
        message.mParams = mList;
        AsynHttpClient.getInstance(this).execUserHttp(message);

    }


}
